package io.bitmax.exchange.login.present;

import android.content.Intent;
import android.text.TextUtils;

import io.bitmax.exchange.R;
import io.bitmax.exchange.base.entity.BaseHttpResult;
import io.bitmax.exchange.base.observer.BaseObserver;
import io.bitmax.exchange.base.service.RetrofitService;
import io.bitmax.exchange.login.entity.User;
import io.bitmax.exchange.login.model.LoginModel;
import io.bitmax.library.mvp.BasePresenter;
import io.bitmax.library.mvp.IBaseUI;
import io.bitmax.library.utils.JsonUtils;
import io.bitmax.library.utils.ToastUtils;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by gongdongyang on 2018/9/25.
 */

public class LoginPresenter extends BasePresenter<LoginPresenter.LoginUI> {

    public interface  LoginUI extends IBaseUI {

    }

    private LoginModel mLoginModel;

    //private CompositeSubscription msubscription;//管理所有的订阅

    public LoginPresenter() {
        mLoginModel = new LoginModel();
    }

    public void login(String loginName,String password) {

        if(!mLoginModel.checkLogin(loginName,password)){
            return;
        }

        //HttpUiTips.showDialog(getActivity(),true, "..loading...");
        BaseObserver<String> observer = new BaseObserver<String>(getActivity()) {
            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                //HttpUiTips.dismissDialog(getActivity());
                ToastUtils.showToast("onSuccess" );
                /*User user = JsonUtils.fromJson(content,User.class);
                UserSingle._getInstance().setUser( user);*/
                //ARouter.getInstance().build("/com/homeActivity").navigation();
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
                ToastUtils.showToast("onError" );
            }
        };
        /*RetrofitService.login(loginName,password)
                .observeOn(Schedulers.io())
                .map(new Function<BaseHttpResult<String>, User>() {
                    @Override
                    public User apply(BaseHttpResult<String> stringBaseHttpResult) throws Exception {
                        User user = JsonUtils.fromJson(stringBaseHttpResult.getResContent(),User.class);
                        return user;
                    }
                }).flatMap(new Function<User, ObservableSource<BaseHttpResult<String>>>() {
                    public ObservableSource<BaseHttpResult<String>> apply(User user) throws Exception {
                        return null;
                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);*/
        RetrofitService.login(loginName,password).subscribe(observer);

    }



}
